package com.groupThree.service;

import com.groupThree.bean.Express;
import com.groupThree.dao.BaseExpressDao;
import com.groupThree.dao.imp.ExpressDaoMysql;
import com.groupThree.exception.DuplicateCodeException;
import com.groupThree.util.RandomUtil;

import java.util.List;
import java.util.Map;

public class ExpressService {
   private static BaseExpressDao dao =new ExpressDaoMysql();
    /**
     * 用于查询所有快递，待取快递
     *
     * @return [{size:总数，day：新增}，{size:总数,day：新增}]
     */

    public static List<Map<String, Integer>> console() {
        return dao.console();
    }

    /**
     * 查询所有快递
     *
     * @param limit
     * @param offset
     * @param pageNumber
     * @return 快递集合
     */

    public static List<Express> findAll(boolean limit, int offset, int pageNumber) {
        return dao.findAll(limit,offset,pageNumber);
    }

    /**
     * 根据单号查询
     *
     * @param number
     * @return
     */

    public static Express fidByNumber(String number) {
        return dao.fidByNumber(number);
    }

    /**
     * \
     * 根据取件码查询
     *
     * @param code
     * @return
     */

    public static Express fidByCode(String code) {
        return dao.fidByCode(code);
    }

    /**
     * 根据用户手机号查询
     *
     * @param userPhone
     * @return
     */

    public static List<Express> findByUserPhone(String userPhone) {
        return dao.findByUserPhone(userPhone);
    }

    public static List<Express> findByUserPhoneAndStatus(String userPhone,int status){
        return dao.findByUserPhoneAndStatus(userPhone,status);
    }
    /**
     * 根据录入人手机号查询
     *
     * @param sysPhone
     * @return
     */

    public static List<Express> findBySysPhone(String sysPhone) {
        return dao.findBySysPhone(sysPhone);
    }

    /**
     * 快递录入
     *
     * @param e
     * @return
     */

    public static boolean insert(Express e) {
        //生成取件码
        e.setCode(RandomUtil.getCode()+"");

        try {
            boolean flag = dao.insert(e);
            if (flag){
                System.out.println("你的快递已经到达快递驿站,请根据取件码: "+e.getCode()+"  取件！");
            }
            return flag;
        } catch (DuplicateCodeException duplicateCodeException) {
            return insert(e);
        }
    }

    /**
     * 修改快递，传入id，跟新快递对象
     *
     * @param id
     * @param newExpress
     * @return
     */

    public static boolean update(int id, Express newExpress) {
        if (newExpress.getUserPhone()!=null){
            dao.delete(id);
            return insert(newExpress);
        }else {
            boolean update = dao.update(id, newExpress);
            Express express = dao.fidByNumber(newExpress.getNumber());
            if (newExpress.getStatus()==1){
                updateStatus(express.getCode());
            }
            return update;
        }
    }

    /**
     * 更改快递状态
     *
     * @param code
     * @return
     */

    public static boolean updateStatus(String code) {
        return dao.updateStatus(code);
    }

    /**
     * 根据id删除快递
     *
     * @param id
     * @return
     */

    public static boolean delete(int id) {
        return dao.delete(id);
    }
    public static Express findByCode(String code){
        return dao.fidByCode(code);

    }


}
